package com.contacts.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.contacts.pojo.Contact;
import com.contacts.service.ContactService;


public class UpdateContactServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		Contact contact=new Contact();
		try {
			BeanUtils.populate(contact, request.getParameterMap());
			ContactService contactService=new ContactService();
			int i = contactService.updateContact(contact);
			if(i>0){
				request.setAttribute("msg", "修改成功");
				request.getRequestDispatcher("/findall").forward(request, response);
				return ;
			}else{
				request.getRequestDispatcher("/findcontactbyid?id="+contact.getId()).forward(request, response);
				return ;
			}
			
		} catch (Exception e) {
			request.getRequestDispatcher("/findcontactbyid?id="+contact.getId()).forward(request, response);
			e.printStackTrace();
		} 
		
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
